package controller

import (
	"RocFamilySystem/internal/service"
	"RocFamilySystem/models"
	"RocFamilySystem/pkg/constant"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
	"strings"
)

type WorkTravelPayController struct {
	BaseController
	WorkTravelPayService *service.WorkTravelPayService
}

// 初始化
func init() {
	registerContainer["WorkTravelPayController"] = &WorkTravelPayController{}
}

// SetService 设置服务
func (controller *WorkTravelPayController) SetService(workTravelPayService *service.WorkTravelPayService) {
	controller.WorkTravelPayService = workTravelPayService
}

// RegisterRouter 注册路由
func (controller *WorkTravelPayController) RegisterRouter(engine *gin.Engine) {
	prefix := "/workTravelPay"
	g := engine.Group(prefix)
	prefixPermission := "permission" + strings.ReplaceAll(prefix, "/", ":")
	{
		//新增工作出差支付
		g.POST("", controller.AddWorkTravelPay)
		registerPermissionContainer("新增工作出差支付", prefixPermission+":create", prefix, "POST")
		//删除工作出差支付
		g.DELETE("/:id", controller.RemoveWorkTravelPay)
		registerPermissionContainer("删除工作出差支付", prefixPermission+":delete", prefix, "DELETE")
		//修改工作出差支付
		g.PUT("", controller.ModifyWorkTravelPay)
		registerPermissionContainer("修改工作出差支付", prefixPermission+":update", prefix, "PUT")
		//获取工作出差支付列表
		g.POST("/list", controller.QueryWorkTravelPay)
		registerPermissionContainer("查询工作出差支付列表", prefixPermission+":list", prefix, "POST")
		//通过ID查询工作出差支付
		g.GET("/:id", controller.QueryWorkTravelPayById)
		registerPermissionContainer("ID查询工作出差支付", prefixPermission+":id", prefix, "GET")
	}
}

// AddWorkTravelPay 添加工作出差支付
// @Summary AddWorkTravelPay 添加工作出差支付
// @Tags 工作出差支付
// @Produce json
// @Param workTravelPay body models.WorkTravelPay true "工作出差支付"
// @Success 200 {object} Result "成功"
// @Router /workTravelPay [post]
func (controller *WorkTravelPayController) AddWorkTravelPay(ctx *gin.Context) {
	var workTravelPay models.WorkTravelPay
	err := ctx.ShouldBind(&workTravelPay)
	controller.HandleError(err, constant.PARAMS_ERROR)
	controller.WorkTravelPayService.AddWorkTravelPay(&workTravelPay, controller.GetLoginUserId(ctx))
	controller.CustomResult(ctx, 200, "添加工作出差支付成功", nil)
}

// RemoveWorkTravelPay 删除工作出差支付
// @Summary RemoveWorkTravelPay  删除工作出差支付
// @Tags 工作出差支付
// @Produce json
// @Param id path int true "工作出差支付id"
// @Success 200 {object} Result "成功"
// @Router /workTravelPay/:id [delete]
func (controller *WorkTravelPayController) RemoveWorkTravelPay(ctx *gin.Context) {
	workTravelPayId, err := strconv.Atoi(ctx.Param("id"))
	controller.HandleError(err, constant.PARAMS_ERROR)
	controller.WorkTravelPayService.RemoveWorkTravelPay(workTravelPayId, controller.GetLoginUserId(ctx))
	controller.CustomResult(ctx, 200, fmt.Sprintf("删除【%d】成功", workTravelPayId), nil)
}

// ModifyWorkTravelPay 修改工作出差支付
// @Summary ModifyWorkTravelPay 修改工作出差支付
// @Tags 工作出差支付
// @Produce json
// @Param workTravelPay body models.WorkTravelPay true "工作出差支付"
// @Success 200 {object} Result "成功"
// @Router /workTravelPay [put]
func (controller *WorkTravelPayController) ModifyWorkTravelPay(ctx *gin.Context) {
	var workTravelPay models.WorkTravelPay
	err := ctx.ShouldBind(&workTravelPay)
	controller.HandleError(err, constant.PARAMS_ERROR)
	controller.WorkTravelPayService.ModifyWorkTravelPay(&workTravelPay, controller.GetLoginUserId(ctx))
	controller.CustomResult(ctx, 200, fmt.Sprintf("修改【%d】成功", *workTravelPay.Id), nil)
}

// QueryWorkTravelPay 查询工作出差支付列表
// @Summary QueryWorkTravelPay 查询工作出差支付列表
// @Tags 工作出差支付
// @Produce json
// @Param workTravelPay body models.WorkTravelPay true "工作出差支付"
// @Success 200 {object} Result "成功"
// @Router /workTravelPay/list [post]
func (controller *WorkTravelPayController) QueryWorkTravelPay(ctx *gin.Context) {
	pageNum, pageSize := controller.GetPageInfo(ctx)
	var workTravelPay models.WorkTravelPay
	err := ctx.ShouldBind(&workTravelPay)
	controller.HandleError(err, constant.PARAMS_ERROR)
	data := controller.WorkTravelPayService.QueryWorkTravelPay(&workTravelPay, pageNum, pageSize, controller.GetLoginUserId(ctx))
	controller.SuccessResult(ctx, data)
}

// QueryWorkTravelPayById 查询工作出差支付通过ID
// @Summary QueryWorkTravelPayById  查询工作出差支付通过ID
// @Tags 工作出差支付
// @Produce json
// @Param id path int true "工作出差支付id"
// @Success 200 {object} Result "成功"
// @Router /workTravelPay/:id [get]
func (controller *WorkTravelPayController) QueryWorkTravelPayById(ctx *gin.Context) {
	workTravelPayId, err := strconv.Atoi(ctx.Param("id"))
	controller.HandleError(err, constant.PARAMS_ERROR)
	data := controller.WorkTravelPayService.QueryWorkTravelPayById(workTravelPayId, controller.GetLoginUserId(ctx))
	controller.SuccessResult(ctx, data)
}

// RemoveWorkTravelPayBatch 批量删除工作出差支付
// @Summary RemoveWorkTravelPay  批量删除工作出差支付
// @Tags 工作出差支付
// @Produce json
// @Param ids[] query []int true "工作出差支付id"
// @Success 200 {object} Result "成功"
// @Router /workTravelPay/batchDelete [delete]
func (controller *WorkTravelPayController) RemoveWorkTravelPayBatch(ctx *gin.Context) {
	ids := ctx.QueryArray("ids[]")
	if len(ids) == 0 {
		controller.HandleError(errors.New("参数缺失"), constant.PARAMS_ERROR)
	}
	controller.WorkTravelPayService.RemoveWorkTravelPayBatch(ids, controller.GetLoginUserId(ctx))
	controller.CustomResult(ctx, 200, fmt.Sprintf("删除【%v】成功", ids), nil)
}
